package com.xqls.biz.impl;

import java.util.*;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xqls.bean.RoleBean;
import com.xqls.biz.RoleBiz;
import com.xqls.dal.model.Role;
import com.xqls.service.RolePermissionKeyService;
import com.xqls.service.RoleService;
import com.xqls.service.UserRoleKeyService;
import com.xqls.util.CommonPage;
import com.xqls.util.CommonResult;
import com.xqls.util.ObjectUtil;
import com.xqls.util.VelocityUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service
public class RoleBizImpl implements RoleBiz {
	private static Logger logger = Logger.getLogger(RoleBiz.class);

	
	@Autowired
	private RoleService roleService;
	@Autowired private UserRoleKeyService userRoleService;
	@Autowired
	private RolePermissionKeyService rolePermissionService;

	@Override
	public CommonResult insertRecord(RoleBean bean) {
		CommonResult result = new CommonResult();
		Role record = null;

		try {
			if (this.roleService.getListById(bean.getRoleId()).size() > 0) {
				result.setResultMsg("角色编号重复！");
			} else if (this.roleService.getListByName(bean.getRoleName()).size() > 0) {
				result.setResultMsg("角色名称重复！");
			} else {
				record = (Role) ObjectUtil.transfer(bean, Role.class);
				result.setIsSuccess(this.roleService.insertRecord(record));
			}
		} catch (Exception e) {
			logger.error("添加角色异常", e);
		}
		return result;
	}

	@Override
	public CommonResult updateRecord(RoleBean bean) {
		CommonResult result = new CommonResult();
		Role record = null;
		List<Role> list = null;
		try {
			list = this.roleService.getListByName(bean.getRoleName());
			for (Role role : list) {
				if (!role.getRoleId().equals(bean.getRoleId())) {
					result.setResultMsg("角色名称重复！");
					return result;
				}
			}
			
			record = (Role) ObjectUtil.transfer(bean, Role.class);
			result.setIsSuccess(this.roleService.updateRecord(record));
		} catch (Exception e) {
			logger.error("修改角色异常", e);
		}
		return result;
	}

	@Override
	public CommonResult deleteRecord(String roleId) {
		CommonResult result = new CommonResult();
		try {
			/*if (this.rolePermissionService.getListByRoleId(roleId).size() > 0) {
				result.setResultMsg("请删除该角色关联的权限");
			}
			else */if(this.userRoleService.getListByRoleId(roleId).size()>0){
				result.setResultMsg("请删除该角色关联的账户");
			}
			else {
				result.setIsSuccess(this.roleService.deleteRecord(roleId));
			}
		} catch (Exception e) {
			logger.error("删除角色异常", e);
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RoleBean> getList() {
		List<RoleBean> roleList = null;
		try {
			roleList = ObjectUtil.transferList(this.roleService.getList(), Role.class);
		} catch (Exception e) {
			logger.error("查询角色异常", e);
		}
		return roleList;
	}

	@Override
	public RoleBean getById(String roleId) {
		RoleBean bean = null;
		try {
			bean = (RoleBean) ObjectUtil.transfer(this.roleService.getListById(roleId).get(0), RoleBean.class);
		} catch (Exception e) {
			logger.error("查询角色异常", e);
		}
		return bean;
	}

	@Override
	public CommonResult getListByPage(CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
		CommonResult result = new CommonResult();
		try {
			List<Role> roleList=roleService.getListByMap(commonPage.pageToMap());
			List<Map<String,Object>> roleMap =new ArrayList<Map<String, Object>>();
			int curr=commonPage.getCurr();
			for (int i=0;i<roleList.size();i++){
				Map<String,Object> data=new HashMap<String,Object>();
				data.put("roleId",roleList.get(i).getRoleId());
				data.put("roleName",roleList.get(i).getRoleName());
				if(curr==1) {
					data.put("viewId", i + 1);
				}else{
					data.put("viewId",i+1+((curr-1)*15));
				}
				roleMap.add(data);
			}
			commonPage.setPageData(roleMap);
			Map<String, Object> showMap = new HashMap<String, Object>();
			showMap.put("roleList", commonPage.getPageData());
			commonPage.setHtml(VelocityUtil.getHtmlByMap("role/roleListData.vm", showMap,request,response,request.getSession().getServletContext()));
			result.setIsSuccess(true);
		} catch (Exception e) {
			logger.error("查询角色异常", e);
		}
		return result;
	}
}
